package com.microcity.autocomponent;

import android.Manifest;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.microcity.autocomponent.autoview.LBikeProgressBar;
import com.microcity.autocomponent.autoview.Toaster;
import com.microcity.autocomponent.autoview.ratingbar.CustomRatingBar;
import com.microcity.autocomponent.uitls.GifSizeFilter;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.GlideEngine;
import com.zhihu.matisse.filter.Filter;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, CustomRatingBar.OnStarChangeListener {

    private static final int REQUEST_CODE_CHOOSE = 23;

    private ImageView iv_photo;
    private ValueAnimator animator;
    private LBikeProgressBar mBpbUnlockProgress;
    private TextView mTvUnlockProgress;
    private CustomRatingBar mCustomRatingBar;
    private List<Uri> mSelected;
    private Button mBtnVoiceLine;
    private Button mBtnTimePicker;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setListener();
        initUnlock();
    }

    private void initView() {
        mBpbUnlockProgress = (LBikeProgressBar) findViewById(R.id.bpb_unlock_progress);
        mTvUnlockProgress = (TextView) findViewById(R.id.tv_main_progress_value);
        mCustomRatingBar = (CustomRatingBar) findViewById(R.id.rb_pay_comment);
        iv_photo = (ImageView) findViewById(R.id.iv_photo);
        mBtnVoiceLine = (Button) findViewById(R.id.btn_voice_line);
        mBtnTimePicker = (Button) findViewById(R.id.btn_num_picker);
    }

    private void initUnlock() {
        animator = ObjectAnimator.ofFloat(0f, 100f);
        animator.setDuration(6000);
        animator.start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Float progress = (Float) animation.getAnimatedValue();
                mBpbUnlockProgress.setmProgress(progress.intValue());
                mTvUnlockProgress.setText("Unlocking " + progress.intValue() + "%");
                if (progress == 100f) {
                    //开锁成功
                    Toaster.get().show(MainActivity.this,"Unlock success");
                    mBpbUnlockProgress.setmProgress(100);
                }
            }
        });
    }

    private void setListener() {
        iv_photo = (ImageView) findViewById(R.id.iv_photo);
        findViewById(R.id.photo).setOnClickListener(this);
        mBtnVoiceLine.setOnClickListener(this);
        mCustomRatingBar.setOnStarChangeListener(this);
        mBtnTimePicker.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.photo:
                RxPermissions rxPermissions = new RxPermissions(this);
                rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        .subscribe(new Observer<Boolean>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(Boolean aBoolean) {
                                if(aBoolean) {
                                    Matisse.from(MainActivity.this)
                                            //图片／视频格式
                                            .choose(MimeType.ofAll(), false)
                                            //设置主题，默认知乎主题
                                            .theme(R.style.Matisse_Dracula)
                                            //设置选取数自动增加
                                            .countable(true)
                                            //是否带拍照
                                            .capture(true)
                                            //设置保存图片权限策略
                                            .captureStrategy(
                                                    new CaptureStrategy(true, "com.microcity.autocomponent.fileprovider"))
                                            //图片选取最大数
                                            .maxSelectable(1)
                                            //选择item增加过滤器
                                            .addFilter(new GifSizeFilter(320, 320, 5 * Filter.K * Filter.K))
                                            //设置媒体网格的期望大小，以适应不同屏幕大小
                                            .gridExpectedSize(
                                                    getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
                                            //设置Activity方向
                                            .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
                                            //设置图片缩略图
                                            .thumbnailScale(0.85f)
                                            //设置图片引擎，默认Glide
                                            .imageEngine(new GlideEngine())
                                            //开始选取照片、等待结果
                                            .forResult(REQUEST_CODE_CHOOSE);
                                }

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });
                break;
            case R.id.btn_voice_line:
                startActivity(new Intent(this,VoiceActivity.class));
                break;

            case R.id.btn_num_picker:
                startActivity(new Intent(this,NumPickerActivity.class));
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) {
            mSelected = Matisse.obtainResult(data);
            Log.d("Matisse", "mSelected: " + mSelected);
            Glide.with(this).load(mSelected.get(0)).into(iv_photo);
        }
    }

    @Override
    public void onStarChange(CustomRatingBar ratingBar, float star) {
        Toaster.get().show(this,"" + star);
    }
}
